package cn.ydt.ydt.community.service.utils;

import cn.ydt.ydt.common.exception.YdtServiceException;
import cn.ydt.ydt.common.pojo.domain.UserJwtVO;
import cn.ydt.ydt.common.resful.StatusCode;
import cn.ydt.ydt.pojo.admin.model.Net;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityContextUtils {
    public static UserJwtVO getUserInfo(){
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken)
                        SecurityContextHolder.getContext().getAuthentication();
        if (authenticationToken!= null){
            UserJwtVO userJwtVO = (UserJwtVO) authenticationToken.getCredentials();
            return userJwtVO;
        }
        throw new YdtServiceException(StatusCode.LOGIN_USER_FALIED,"没有登录信息");
    }

    //获取当前用户id
    public static Long getUserId(){
        return SecurityContextUtils.getUserInfo().getUserId();
    }

    public static Net getNet(){
        UsernamePasswordAuthenticationToken authenticationToken =
                (UsernamePasswordAuthenticationToken)
                        SecurityContextHolder.getContext().getAuthentication();
        if (authenticationToken!= null){
            Net netInfo = (Net) authenticationToken.getCredentials();
            return netInfo;
        }
        throw new YdtServiceException(StatusCode.LOGIN_USER_FALIED,"没有登录信息");
    }

    //获取当前网格员所在社区id
    public static Long getNetCommunityId(){return SecurityContextUtils.getNet().getCommunityId();}
}
